package com.example.manager.service.Imple;


import com.example.manager.entity.User;
import com.example.manager.entity.UserPageBean;
import com.example.manager.mapper.UserMapper;
import com.example.manager.service.IUserService;
import com.example.manager.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService implements IUserService {
    @Autowired
    private UserMapper userMapper;


    @Override
    public Integer addUser(String username) {
        return null;
    }

    //用户登录
    @Override
    public JsonResult<User> login(User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        JsonResult<User>jsonResult = new JsonResult<>(100);
        User user1 =  userMapper.findByUsername(username);
        if(user1==null){
            jsonResult.setMessage("用户不存在!");
            jsonResult.setState(140);
        } else if (!user1.password.equals(password)) {
            jsonResult.setMessage("密码错误");
            jsonResult.setState(150);
        }
        System.out.println(jsonResult.toString());
        jsonResult.setData(user1);
        return jsonResult;

    }


    //用户注册
    @Override
    public Integer register(User user,String repassword) {
        User user1 = userMapper.findByUsername(user.username);
        if (user1!=null){
            return 120;
        }else if (user.username.length()<2){
            return 110;
        }else if (repassword.equals(user.password)){
            userMapper.insert(user);
        }else {
            return 130;
        }

            return 100;
    }


    //按用户名查找用户
    @Override
    public UserPageBean findByUsername(String search,Integer pageSize) {
        UserPageBean pageBean = new UserPageBean();
        List<User>users = userMapper.findUserByUsername(search);
        int totalSize = userMapper.getAllUserSize1(search);
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(1);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setUsers(users);

        return pageBean;
    }

    @Override
    public Integer getAllUserSize() {

        int total = 0;
        total = userMapper.getAllUserSize();

        return total;
    }

    @Override
    public UserPageBean getUserByPage(Integer currentpage, Integer pageSize) {
        UserPageBean pageBean = new UserPageBean();
        List<User>users = userMapper.getUserByPage((currentpage -1)*5,pageSize);
        int totalSize = userMapper.getAllUserSize();
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(currentpage);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setUsers(users);


        return pageBean;
    }

    @Override
    public Integer delUser(String username) {
        User user=new User();
        userMapper.delUser(username);

        return null;
    }

    @Override
    public User findUserByUsername(String username) {
        User user = new User();
        user = userMapper.findByUsername(username);

        return user;
    }

    @Override
    public void pudateUserInfo(User user) {
        userMapper.updateUserInfo(user);
    }

    @Override
    public Integer updatepw(String password, String newpassword, String repassword, String username) {
        User user = userMapper.findByUsername(username);

        if (!newpassword.equals(repassword)){
            return 210;
        }else if (!user.password.equals(password)){
            return 220;
        }

        userMapper.updatepw(username,password,newpassword);

        return 200;
    }
}
